package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.arco.SelectOption;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysForm;
import com.morningstar.system.service.SysFormService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

@Slf4j
@RestController
@RequestMapping(value = "/system/form")
public class SysFormController {

    private static final String PERMISSION = "system.form";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private SysFormService formService;


    @PostMapping(value = "/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysForm>> list(@RequestBody CommonQuery<SysForm, SysForm> commonQuery) {
        SysForm sysForm = commonQuery.getQuery();
        LambdaQueryWrapper<SysForm> queryWrapper = new LambdaQueryWrapper<>();
        if (sysForm != null) {
            queryWrapper.like(StringUtils.isNotBlank(sysForm.getFormName()), SysForm::getFormName, sysForm.getFormName());
        }
        queryWrapper.orderByAsc(SysForm::getCreateTime);
        return Result.successData(formService.page(commonQuery.getPage(), queryWrapper));
    }

    @PostMapping
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody SysForm sysForm) {
        ServiceCheckResult checkResult = formService.checkAddAndUpdate(sysForm);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        formService.save(sysForm);
        return Result.successNoData();
    }

    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody SysForm sysForm) {
        ServiceCheckResult checkResult = formService.checkAddAndUpdate(sysForm);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        formService.updateById(sysForm);
        return Result.successNoData();
    }

    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("formId") Long formId) {
        formService.removeById(formId);
        return Result.successNoData();
    }

    @GetMapping("/detail")
    @SaCheckLogin
    public Result<SysForm> detail(@RequestParam("formId") Long formId) {
        return Result.successData(formService.getById(formId));
    }

    @GetMapping("/select")
    @SaCheckLogin
    public Result<List<SelectOption>> select() {
        return Result.successData(SelectOption.getSelectOptionList(formService.list()));
    }
}
